package cn.edu.scau.cmi.zhengweibo.client.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.edu.scau.cmi.zhengweibo.adapter.AdapterOfClass;
import cn.edu.scau.cmi.zhengweibo.adapter.AdapterOfObject;
import cn.edu.scau.cmi.zhengweibo.adapter.Student;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class AdapterViewController{

	@FXML
	private Button classButton;
	
	@FXML
	private Button objectButton;
	
	@FXML
	private TextField idText;
	
	@FXML
	private TextField nameText;
	
	@FXML
	private Label label;
	
	ArrayList<Integer> intList = new ArrayList<Integer>(); 
	Map<Integer,Student> Students = new HashMap<>();
	AdapterOfClass AdapterOfClass = new AdapterOfClass();
	AdapterOfObject AdapterOfObject = new AdapterOfObject();
	int flag = 0;
	
	public void classAdapterOfStudent(ActionEvent event) {
		String text = "";
		String stuId = idText.getText();
		String stuName = nameText.getText();
		if(!intList.contains(Integer.parseInt(stuId))) {
			Students.put(Integer.parseInt(stuId), new Student(Integer.parseInt(stuId), stuName));
			intList.add(Integer.parseInt(stuId));
		}
		
		if(flag == 1) {
			intList.clear();
			Students.clear();
			flag = 0;
		}
		intList = (ArrayList<Integer>) AdapterOfClass.sortInt(intList);
		text+="学生学号			 学生姓名\n";
		for(int i = 0;i<intList.size();i++) {
        	text+=intList.get(i)+"		                 "+Students.get(intList.get(i)).getName()+"\n";
        }
        label.setText(text);
		
	}
	
	public void objectAdapterOfStudent(ActionEvent event) {
		String text = "";
		String stuId = idText.getText();
		String stuName = nameText.getText();
		if(!intList.contains(Integer.parseInt(stuId))) {
			Students.put(Integer.parseInt(stuId), new Student(Integer.parseInt(stuId), stuName));
			intList.add(Integer.parseInt(stuId));
		}
		if(flag == 0) {
			intList.clear();
			Students.clear();
			flag = 1;
		}
		intList = (ArrayList<Integer>) AdapterOfObject.sortInt(intList);
		text+="学生学号			 学生姓名\n";
		for(int i = 0;i<intList.size();i++) {
        	text+=intList.get(i)+"		                 "+Students.get(intList.get(i)).getName()+"\n";
        }
        label.setText(text);
	}

}
